import { Body, Controller, Get, Ip, Post, Req } from "@nestjs/common";
import { ApiBearerAuth, ApiOperation, ApiTags } from "@nestjs/swagger";

import { SkipJwtAuth } from "@/core/auth/decorator/skip-auth.decorator";
import { ApiBaseResponse } from "@/core/decorators/api-base-response.decorator";
import AjaxResult from "@/core/utils/AjaxResult";

import { LoginDto } from "./dto/login.dto";
import { RegisterDto } from "./dto/register.dto";
import { LoginService } from "./login.service";
import { LoginVo } from "./vo/login.vo";

@ApiTags("登录注册")
@Controller("/")
export class LoginController {
    constructor(private readonly loginService: LoginService) {}

    @ApiOperation({ summary: "账号登录" })
    @SkipJwtAuth()
    @ApiBaseResponse(LoginVo)
    @Post("/account/login")
    async login(@Body() loginDto: LoginDto, @Ip() ip: string) {
        const loginVo = await this.loginService.login(loginDto.username, loginDto.password, ip);
        return AjaxResult.success(loginVo);
    }

    @ApiOperation({ summary: "登出" })
    @ApiBearerAuth()
    @Get("/logout")
    async logout(@Req() req) {
        await this.loginService.logout(req.user.id);
        return AjaxResult.success();
    }

    @ApiOperation({ summary: "账号注册" })
    @SkipJwtAuth()
    @Post("/account/register")
    async register(@Body() registerDto: RegisterDto) {
        await this.loginService.register(registerDto.username, registerDto.password, registerDto.channel);
        return AjaxResult.success();
    }
}
